﻿$(function () {
    //2.初始化页面
    var oPageInit = new PageInit();
    oPageInit.Init();
    oPageInit.LoadType("type", "#sel_type", '', true);

});

var PageInit = function () {
    var oInit = new Object();
    var postdata = {};

    var table, form, layer;
    var active;
    var tableId = 'tb_list';
    //初始化页面上面的按钮事件
    oInit.Init = function () {
        layui.use('table', function () {
            table = layui.table
          , form = layui.form
          , layer = layui.layer;
            oInit.loadData();
        });
    }

    //初始化Table,加载数据
    oInit.loadData = function () {
        //方法级渲染
        table.render({
            elem: '#' + tableId
            , id: tableId
        , loading: true
        , toolbar: '#toolbar'
        , defaultToolbar: ['']  //工具栏右侧的图标按钮
        , url: Root + 'FZParam/GetBaseParam'
        , cols: [[
        { checkbox: true, fixed: true }, { type: 'numbers', title: '序号' }
        , { field: 'id', title: 'ID', sort: true, fixed: true, hide: true }
        , {
            field: 'name',
            title: '参数名称'
        }, {
            field: 'descs',
            title: '描述'
        }, {
            field: 'type',
            title: '参数类别',
            formatter: function (value, row, index) {
                return ShowParamType(value);
            }
        }
            , { fixed: 'right', title: '操作', toolbar: '#tableBar', width: 120 }
        ]]
        , page: true
        , limit: 10
            //, cellMinWidth: 80
        , height: 'full-300'
        });

        var $ = layui.$;
        active = {
            reload: function () {
                var txt_search = $('#txt_search');
                var temp = {
                    SearchText: txt_search.val(),
                    type: sel_type.getValue('value'),
                };

                //执行重载
                table.reload(tableId, {
                    method: 'post',
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                  , where: temp
                }, 'data');
            }
        };

        $('#formSearch .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        zpActive.tableTool(table, layer, form, null, null, null, null, null, oInit.Delete, oInit.LoadTypeInit);

        //监听提交
        form.on('submit(save)', function (data) {
            var rs = oInit.Save(data.field);    //保存数据
            if (rs) {
                active.reload();    //重新加载table数据
                layer.closeAll();   //关闭弹出层
            }
            return false;   //true刷新当前页面
        });
    }

    //保存-新增
    oInit.Save = function (formdata) {
        //var dto = decodeURIComponent(formdata, true);   //处理中文,后台需要解码
        var result = false;
        //静态添加节点
        $.ajax({
            type: "post",
            url: Root + 'FZParam/SaveBaseParam',
            async: false,
            data: { dto: formdata },
            success: function (data) {
                if (data.SubmitResult) {
                    result = true;
                } else {
                    dialog.danger('操作失败！Error Message:' + data.Message);
                }
            },
            error: function () {
                dialog.danger('操作失败！请重试或者联系管理员');
            },
            complete: function () {

            }
        });
        return result;
    }

    //删除
    oInit.Delete = function (arr_obj) {
        $.ajax({
            type: "post",
            url: Root + 'FZParam/DeleteBaseParam',
            data: { dto: arr_obj },
            success: function (data) {
                if (data.SubmitResult) {
                    dialog.success('删除成功');
                    active.reload();
                } else {
                    dialog.danger('操作失败！Error Message:' + data.Message);
                }
            },
            error: function () {
                dialog.danger('操作失败！请重试或者联系管理员');
            },
            complete: function () {

            }
        });
    }

    //加载下拉参数类别数据
    let sel_type;
    oInit.LoadType = function (type, selId, typeValue, isMultiple) {
        typeValue = typeValue ? typeValue : "";
        $.ajax({
            type: "post",
            async: false,
            url: Root + 'FZParam/GetAllBaseParam',
            data: { type: type },
            success: function (data) {
                //selId = selId ? selId : '#selType,#sel_type';
                if (!selId || selId.length == 0) {
                    return;
                }

                var selectData = new Array();
                var select = $(selId);
                select.empty();
                $.each(data.data, function (i, item) {
                    var name = ShowParamType(item.type);
                    if (isMultiple) {
                        selectData.push({ name: name, value: item.type });
                    } else {
                        select.append('<option value="' + item.type + '">' + name + '</option>');
                    }
                });
                //select.val(typeValue);
                //select.selectpicker('refresh');
                if (isMultiple) {
                    sel_type = xmSelect.render({
                        el: selId,
                        tips: '请选择参数类别',
                        language: 'zn',
                        data: selectData
                    })
                }
            },
            error: function () {
                dialog.danger('操作失败！请重试或者联系管理员');
            },
            complete: function () {

            }
        });
    }

    oInit.LoadTypeInit = function () {
        oInit.LoadType('type', '#selType');    //加载下拉角色
    }

    return oInit;
}